package cn.edu.scau.cmi.linhongxin.client;
import javax.persistence.criteria.CriteriaBuilder.Case;

import cn.edu.scau.cmi.linhongxin.abstractFactory.*;
import cn.edu.scau.cmi.linhongxin.simpleFactoryMethod.Beef;
import cn.edu.scau.cmi.linhongxin.simpleFactoryMethod.Chicken;
import cn.edu.scau.cmi.linhongxin.simpleFactoryMethod.MeatFactory;
import cn.edu.scau.cmi.linhongxin.simpleFactoryMethod.Pork;
import cn.edu.scau.cmi.linhongxin.simpleFactoryMethod.Produce;
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.TextArea;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

public class AbstractFactoryClient extends Application{
	@FXML
	private AnchorPane anchorPane;
	@FXML
	private TextArea creatOutput;
	@FXML
	private Button button;
	@FXML
	private ChoiceBox<String> choiceMeatBox;
	@FXML
	private ChoiceBox<String> choiceBrandBox;
	private String nowMeat;
	private String nowBrand;
	private final String[] chooseMeatType = {"pork", "beef", "chicken"};
	private final String[] chooseBrandType = {"Aonong", "TianBang", "Wens"};
	@FXML
	public  void initialize() {	
		
		ObservableList<String> meatList = FXCollections.observableArrayList(chooseMeatType);
		nowMeat = chooseMeatType[0];
		choiceMeatBox.setItems(meatList);
		choiceMeatBox.getSelectionModel().selectFirst();
		choiceMeatBox.getSelectionModel().selectedIndexProperty().addListener(
			new ChangeListener<Number>() {
				@Override
				public void changed(ObservableValue<? extends Number> observable, Number oldValue,
						Number newValue) {
					nowMeat = chooseMeatType[newValue.intValue()];
				}
			});
		ObservableList<String> brandList = FXCollections.observableArrayList(chooseBrandType);
		nowBrand = chooseBrandType[0];
		choiceBrandBox.setItems(brandList);
		choiceBrandBox.getSelectionModel().selectFirst();
		choiceBrandBox.getSelectionModel().selectedIndexProperty().addListener(
			new ChangeListener<Number>() {
				@Override
				public void changed(ObservableValue<? extends Number> observable, Number oldValue,
						Number newValue) {
					nowBrand = chooseBrandType[newValue.intValue()];
				}
			});
		listenButton();
	}
public void listenButton() {
		
		button.setOnMouseClicked(new EventHandler<MouseEvent>() {			
			@Override
			public void handle(MouseEvent arg) {
/* 				if(nowMeat.compareTo(chooseType[0]) == 0) {
//					creatOutput.setText("dvdvd");
					return;
				}*/
				creatOutput.clear();
				String meString = "";
				if (nowBrand.equals("Aonong")) {
					AonongFactory aonongFactory = new AonongFactory();
					switch(nowMeat) {
					case "pork":
						AonongPork aonongpork = aonongFactory.producePork();
						meString = aonongpork.cook();
						break;
					case "beef":
						AonongBeef aonongBeef = aonongFactory.produceBeef();
						meString = aonongBeef.cook();
						break;
					case "chicken":
						AonongChicken aonongChicken = aonongFactory.produceChicken();
						meString = aonongChicken.cook();
					    break;
					}
				}else if (nowBrand.equals("TianBang")) {
					 TianBangFactory tianbangFactory = new TianBangFactory();
					 switch(nowMeat) {
						case "pork":
							TianBangPork tianbangpork = tianbangFactory.producePork();
							meString = tianbangpork.cook();
							break;
						case "beef":
							TianBangBeef tianbangBeef = tianbangFactory.produceBeef();
							meString = tianbangBeef.cook();
							break;
						case "chicken":
							TianBangChicken tianbangChicken = tianbangFactory.produceChicken();
							meString = tianbangChicken.cook();
						    break;
						}   
				}else {
					 WensFactory wensFactory = new WensFactory();
					 switch(nowMeat) {
						case "pork":
							 WensPork wensgpork = wensFactory.producePork();
							 meString = wensgpork.cook(); 
							break;
						case "beef":
							 WensBeef wensBeef = wensFactory.produceBeef();
							 meString = wensBeef.cook();
							break;
						case "chicken":
							WensChicken wensChicken = wensFactory.produceChicken();
							meString = wensChicken.cook();
						    break;
						}     	       
				}
				creatOutput.appendText(meString+'\n');
				
			}
		});
		
	}
	private Stage PrimaryStage;
	private AnchorPane rootLayout;
	@Override
	public void start(Stage primaryStage) {
		try {
			this.PrimaryStage = primaryStage;
			this.PrimaryStage.setTitle("抽象工厂模式");			
			initRootLayout();		
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	public void initRootLayout() {
		try {
			FXMLLoader loader = new FXMLLoader();
			loader.setLocation(AbstractFactoryClient.class.getResource("/cn/edu/scau/cmi/linhongxin/abstractFactory/AbstractFactoryStage.fxml"));
			rootLayout = (AnchorPane) loader.load();		
			Scene scene = new Scene(rootLayout,844,520);
			PrimaryStage.setScene(scene);
			PrimaryStage.show();
			
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		launch(args);
	}
	/*public static void main(String[] args) {
		// TODO Auto-generated method stub
		AonongFactory aonongFactory = new AonongFactory();
       
        Pork aonongpork = aonongFactory.producePork();
        aonongpork.cook();
        Beef aonongBeef = aonongFactory.produceBeef();
        aonongBeef.cook();
        Chicken aonongChicken = aonongFactory.produceChicken();
        aonongChicken.cook();

        TianBangFactory tianbangFactory = new TianBangFactory();
        
        Pork tianbangpork = tianbangFactory.producePork();
        tianbangpork.cook();
        Beef tianbangBeef = tianbangFactory.produceBeef();
        tianbangBeef.cook();
        Chicken tianbangChicken = tianbangFactory.produceChicken();
        tianbangChicken.cook();

        WensFactory wensFactory = new WensFactory();
       
        Pork wensgpork = wensFactory.producePork();
        wensgpork.cook(); 
        Beef wensBeef = wensFactory.produceBeef();
        wensBeef.cook();
        Chicken wensChicken = wensFactory.produceChicken();
        wensChicken.cook();

	}*/

}
